package com.oscar.ehome.view.pageview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.base.fragment.adapter.CustomFragmentPagerAdapter;
import com.oscar.ehome.base.fragment.adapter.NoPreloadViewPager;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.view.messageview.IssueMessageActivity;
import com.oscar.ehome.view.messageview.MessageDetilActivity;
import com.oscar.ehome.view.messageview.MessageFragment;
import com.oscar.ehome.view.setview.SetFragment;
import com.oscar.ehome.view.teamview.TeamFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/7/21
 * 会员页
 */

public class MembersListActivity extends BasesActivity implements NoPreloadViewPager.OnPageChangeListener, CompoundButton.OnCheckedChangeListener {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 全部
     */
    @BindView(R.id.member_all)
    RadioButton memberAll;
    /**
     * 已认证
     */
    @BindView(R.id.member_approve)
    RadioButton memberApprove;
    /**
     * 未认证
     */
    @BindView(R.id.member_noapprove)
    RadioButton memberNoapprove;
    /**
     *
     */
    @BindView(R.id.memberitem_tabs_rg)
    RadioGroup memberitemTabsRg;
    /**
     * pager
     */
    @BindView(R.id.memberlist_frg)
    NoPreloadViewPager memberlistFrg;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    CustomFragmentPagerAdapter adapter = null;
    private int currIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memberlist);
        ButterKnife.bind(this);
        setColor();
        initView();
        onClicks();
        initPager();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(MembersListActivity.this).getSystemConfig().getColor()));
        memberAll.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        memberApprove.setTextColor(Color.parseColor("#333333"));
        memberNoapprove.setTextColor(Color.parseColor("#333333"));
        TintManager.setTint(memberAll, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTint(memberApprove, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTint(memberNoapprove, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
    }

    /**
     * 初始数据
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("准会员");
    }

    /**
     * 点击事件
     */
    public void onClicks() {
        memberAll.setOnCheckedChangeListener(this);
        memberApprove.setOnCheckedChangeListener(this);
        memberNoapprove.setOnCheckedChangeListener(this);
        memberlistFrg.setOnPageChangeListener(this);
    }

    /**
     * 加载页面
     */
    public void initPager() {
        fragmentList.add(new MembersAllFragment());
        fragmentList.add(new MemberApproveFragment());
        fragmentList.add(new MembernoApproveFragment());
        adapter = new CustomFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        memberlistFrg.setAdapter(adapter);
    }

    @OnClick(R.id.title_layout_left)
    public void onClicked() {
        finish();
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (memberAll.isChecked()) {
            memberlistFrg.setCurrentItem(0);
        } else if (memberApprove.isChecked()) {
            memberlistFrg.setCurrentItem(1);
        } else if (memberNoapprove.isChecked()) {
            memberlistFrg.setCurrentItem(2);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                if (currIndex == 1) {//从页卡1跳转转到页卡2
                    memberAll.setChecked(true);
                    memberApprove.setChecked(false);
                    memberNoapprove.setChecked(false);
                    memberAll.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                    memberApprove.setTextColor(Color.parseColor("#333333"));
                    memberNoapprove.setTextColor(Color.parseColor("#333333"));
                } else if (currIndex == 2) {// 从页卡1跳转转到页卡3
                    memberAll.setChecked(true);
                    memberApprove.setChecked(false);
                    memberNoapprove.setChecked(false);
                    memberAll.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                    memberApprove.setTextColor(Color.parseColor("#333333"));
                    memberNoapprove.setTextColor(Color.parseColor("#333333"));
                }
                break;
            case 1:
                if (currIndex == 0) {//从页卡1跳转转到页卡2
                    memberAll.setChecked(false);
                    memberApprove.setChecked(true);
                    memberNoapprove.setChecked(false);
                    memberAll.setTextColor(Color.parseColor("#333333"));
                    memberApprove.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                    memberNoapprove.setTextColor(Color.parseColor("#333333"));
                } else if (currIndex == 2) { // 从页卡1跳转转到页卡2
                    memberAll.setChecked(false);
                    memberApprove.setChecked(true);
                    memberNoapprove.setChecked(false);
                    memberAll.setTextColor(Color.parseColor("#333333"));
                    memberApprove.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                    memberNoapprove.setTextColor(Color.parseColor("#333333"));
                }
                break;
            case 2:
                if (currIndex == 0) { //从页卡1跳转转到页卡2
                    memberAll.setChecked(false);
                    memberApprove.setChecked(false);
                    memberNoapprove.setChecked(true);
                    memberAll.setTextColor(Color.parseColor("#333333"));
                    memberApprove.setTextColor(Color.parseColor("#333333"));
                    memberNoapprove.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                } else if (currIndex == 1) {// 从页卡1跳转转到页卡2
                    memberAll.setChecked(false);
                    memberApprove.setChecked(false);
                    memberNoapprove.setChecked(true);
                    memberAll.setTextColor(Color.parseColor("#333333"));
                    memberApprove.setTextColor(Color.parseColor("#333333"));
                    memberNoapprove.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                }
                break;
        }
        currIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
